package com.guguskins.back.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.guguskins.back.entity.GuRecord;
import com.guguskins.back.entity.GuUser;
import com.guguskins.back.entity.vo.PageVo;
import com.guguskins.back.mapper.GuRecordMapper;
import com.guguskins.back.service.GuRecordService;
import com.guguskins.back.service.GuUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author mybatis-plus
 * @since 2021-05-24
 */
@Service
public class GuRecordServiceImpl extends ServiceImpl<GuRecordMapper, GuRecord> implements GuRecordService {

    @Autowired
    private GuRecordMapper recordMapper;

    @Autowired
    private GuUserService userService;

    @Override
    public void addRecord(long userId, int recordType, int sourceType, long sourceId, int itemType, long itemId, String recordDesc, String recordRemark) {
        GuRecord record = new GuRecord();
        record.setRecordUserId(userId);
        GuUser user = userService.getById(userId);
        record.setRecordSteamId(user.getUserSteamId());
        record.setRecordUserNickname(user.getUserNickname());
        record.setRecordType(recordType);
        record.setRecordSourceType(sourceType);
        record.setRecordSourceId(sourceId);
        record.setRecordItemType(itemType);
        record.setRecordId(itemId);
        record.setRecordDesc(recordDesc);
        record.setRecordRemark(recordRemark);
        record.insert();
    }

    @Override
    public PageVo<GuRecord> findByPage(Integer pageNum, Integer pageSize, String userName, Integer sourceType, String createTime) {
        if (sourceType == -1) {
            sourceType = null;
        }
        if (pageNum == 1) {

        } else {
            pageNum = (pageNum - 1) * pageSize;
        }
        if (createTime != null) {
            createTime = createTime.substring(0, 10);
        }

        return new PageVo<GuRecord>(recordMapper.getByPage(pageNum, pageSize, userName, sourceType, createTime), recordMapper.getByCount());
    }
}
